﻿<Serializable()> Public Class ReadonlyShapeLayer
    Inherits Layer



    Protected textList As New List(Of FText)

    Public Sub New(ByVal ln As String, ByVal fn As String)
        MyBase.new(ln, fn)

    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub

    Dim m_iDraw As Integer = 0
    Protected Overrides Sub DrawText()

        mapwin.ClearDrawing(m_iDraw)


        m_iDraw = mapwin.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList)

        mapwin.set_DrawingLabelsScale(m_iDraw, True)


        For Each dbt As FText In textList


            mapwin.AddDrawingLabel(m_iDraw, dbt.str, 1000, dbt.x, dbt.y, MapWinGIS.tkHJustification.hjLeft)

        Next

    End Sub

    Public Overrides Sub InitLayer(ByRef m As AxMapWinGIS.AxMap)
        mapwin = m



        Dim sf As New MapWinGIS.Shapefile()
        sf.Open(fname)
        m_iLayer = mapwin.AddLayer(sf, True)

        mapwin.set_LayerKey(m_iLayer, lname)

        Me.InitProp()

        Me.DrawText()


    End Sub

    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)
        Dim fn As String = OutPath + lname + ".shp"

        Dim myPath(2) As String
        myPath(0) = OutPath + lname + ".shp"
        myPath(1) = OutPath + lname + ".dbf"
        myPath(2) = OutPath + lname + ".shx"


        For i As Integer = 0 To myPath.Length - 1
            If System.IO.File.Exists(myPath(i)) Then
                Try
                    System.IO.File.SetAttributes(myPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(myPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim sf As New MapWinGIS.Shapefile
        sf.Open(FnameProperty)

        sf.SaveAs(fn)

        sf.Close()

        Me.FnameProperty = fn


        Me.InitLayer(m)


    End Sub

    '保存和加载
    Public Overrides Sub Save()

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        sf.StartEditingShapes()
        Dim bS As Boolean = sf.Save()
        sf.StopEditingShapes()
        If Not bS Then
            System.Windows.Forms.MessageBox.Show("文件保存错误，请查看相应权限!")

        End If


    End Sub

    Public Overrides Sub SaveAs(ByVal strPath As String)

        Dim fn As String = strPath + lname + ".shp"

        Dim myPath(2) As String
        myPath(0) = strPath + lname + ".shp"
        myPath(1) = strPath + lname + ".dbf"
        myPath(2) = strPath + lname + ".shx"


        For i As Integer = 0 To myPath.Length - 1
            If System.IO.File.Exists(myPath(i)) Then
                Try
                    System.IO.File.SetAttributes(myPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(myPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)

        Dim bS As Boolean = sf.SaveAs(fn)

        If bS Then
            System.Windows.Forms.MessageBox.Show("文件保存错误，请查看相应权限!")

        End If


    End Sub

    Public Overrides Sub ReLoad(ByRef m As AxMapWinGIS.AxMap, ByVal fPath As String)

        fname = fPath + lname + ".shp"

        Me.InitLayer(m)



    End Sub

    Public Overrides Sub Dispose()

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        sf.Close()

    End Sub

    Public Overrides Sub InitProp()

    End Sub
End Class
